/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imagetabpaine;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 *
 * @author rbs
 */
public class ImagePane extends JPanel{
    ArrayList <Tab> tabs;
    ArrayList <JButton> buttons;
    int actualTab;
    int width;

    //COLORS:
    private Color back_color; //backround behind the tabs, mostly for borders
    private Color tab_color; // Color of the tabs if not selected
    private Color tab_panel_back_color; //
    private final Color selectColor;


    private Boolean horizontal; //if is NORTH or SOUTH

    private JPanel tab_panel;



    //thin border directly under tabs
    private int tab_border;
    private ArrayList<JPanel> border_panels = new ArrayList<JPanel>();
    private ArrayList<JPanel> topBorderCollection = new ArrayList<JPanel>();
    private JPanel border_panel = new JPanel(); //Panel welches die Lücken zwischen den panelborders verdeckt

    private final JPanel borderContainer; // for south and nord border
    JPanel topBorders; //Panel above the selected tab
    private int panelborder; //Big South border

    protected javax.swing.event.EventListenerList listenerList =
    new javax.swing.event.EventListenerList();


    

    public ImagePane(final ArrayList<Tab> tabs, String tabs_location, final int size,
            int startTab, final Color back_color, Color tab_color, Color selColor,int tab_border, int panelborder,
            Boolean topBorderEnabled) {

        this.setLayout(new BorderLayout());

        this.panelborder = panelborder;

        borderContainer = new JPanel();
        borderContainer.setLayout(new BorderLayout());

        this.tab_border = tab_border;

        if (selColor != null)
            this.selectColor = selColor;
        else
            this.selectColor = tabs.get(0).getPanel().getBackground();

        if (tabs_location == BorderLayout.NORTH || tabs_location == BorderLayout.SOUTH)
            this.horizontal = true;
        else
            this.horizontal = false;
        
        border_panel = new JPanel();
        border_panel.setOpaque(true);
        border_panel.setBackground(back_color);

        if (horizontal)
        {
            border_panel.setPreferredSize(new Dimension(0, tab_border));
            border_panel.setLayout(new GridLayout(1, tabs.size(), tab_border, tab_border));
        }
        else
        {
            border_panel.setPreferredSize(new Dimension(tab_border, 0));
            border_panel.setLayout(new GridLayout(tabs.size(), 1, tab_border, tab_border));
        }

        for (int i = 0; i < tabs.size(); i++)
        {
            JPanel border = new JPanel();
            border.setBackground(back_color);
            border_panels.add(border);
            border_panel.add(border);
        }

        //The Panel wich really displays the tabs
        tab_panel = new JPanel();


        //the border between the tabs and the main panel
        JPanel bigBorder = new JPanel();
        bigBorder.setPreferredSize(new Dimension(panelborder, panelborder));
        bigBorder.setBackground(tabs.get(0).getPanel().getBackground());
        borderContainer.add (border_panel, tabs_location);
        borderContainer.add(bigBorder, BorderLayout.CENTER);

        //the border on the top
        if (true) //TODO
        {
            topBorders = new JPanel();
            topBorders.setLayout(new GridLayout(1, tabs.size(), tab_border, tab_border));
            topBorders.setBackground(tab_color);
            topBorders.setPreferredSize(new Dimension(tab_border, tab_border));
            this.add(topBorders, tabs_location);
            topBorders.setBackground(back_color);
            for (int i = 0; i < tabs.size(); i++)
            {
                JPanel border = new JPanel();
                border.setBackground(tab_color);
                topBorderCollection.add(border);
                topBorders.add(border);
            }
            //this.add(topBorders, BorderLayout.NORTH);
        }

        this.add(tab_panel, BorderLayout.CENTER);

        if (tabs_location.equals(BorderLayout.NORTH))
        {
            this.add(topBorders, BorderLayout.NORTH);
            this.add(borderContainer, BorderLayout.SOUTH);
        }
        else if(tabs_location.equals(BorderLayout.WEST))
            this.add(borderContainer, BorderLayout.EAST);
        else if(tabs_location.equals(BorderLayout.SOUTH))
            this.add(borderContainer, BorderLayout.NORTH);
        else if(tabs_location.equals(BorderLayout.EAST))
            this.add(borderContainer, BorderLayout.WEST);

        //UIManager.put("Button.select", Color.black);

        double tableSize[][] = new double[tabs.size() + 1][2];


        tab_panel.setPreferredSize(new Dimension(size, size));
        //tab_panel.setLayout(new GridBagLayout());
        if (horizontal)
        {
            tab_panel.setPreferredSize(new Dimension(0, size));
            tab_panel.setLayout(new GridLayout(1, tabs.size(), tab_border, 0));
        }
        else
        {
            tab_panel.setPreferredSize(new Dimension(size, 0));
            tab_panel.setLayout(new GridLayout(tabs.size(), 1, 0, tab_border));
        }

        buttons = new ArrayList<JButton>();
        this.tabs = tabs;


        actualTab = startTab;
        this.back_color = back_color;
        this.tab_color = tab_color;

        tab_panel.setBackground(back_color);
        
        
        for (int i = 0; i < tabs.size(); i++)
        {
            final JButton button;
            final Tab tab = tabs.get(i);
            //if (horizontal)
                //button = new JButton("<html><center>" + tab.getName(), tab.getIcon());
            //else
            button = new JButton("<html>" + tab.getName(), tab.getIcon());

            if (horizontal)
            {
                button.setHorizontalTextPosition(JButton.CENTER);
                button.setVerticalTextPosition(JButton.BOTTOM);
            }
            else
                 button.setHorizontalTextPosition(JButton.RIGHT);

            button.setBackground(tab_color);
            final int tab_num = i;
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    resetButtonColors();
                    button.setBackground(selectColor);
                    button.setForeground(tab_panel.getBackground());
                    border_panels.get(tab_num).setBackground(selectColor);
                    topBorderCollection.get(tab_num).setBackground(back_color);

                    Object[] listeners = listenerList.getListenerList();
                    // Each listener occupies two elements - the first is the listener class
                    // and the second is the listener instance
                    for (int i=0; i<listeners.length; i+=2) {
                        if (listeners[i] == ImagePaneEventListener.class) {
                            ((ImagePaneEventListener)listeners[i+1]).onAction(new ImagePaneEvent(button, tab_num));
                        }
                    }
                }
            });

            button.setBorderPainted(false);
            button.setFocusPainted(false);

            if (i == startTab)
            {
                button.setContentAreaFilled(true);
                button.setBackground(selectColor);
                button.setForeground(tab_panel.getBackground());
                border_panels.get(i).setBackground(selectColor);
                topBorderCollection.get(i).setBackground(back_color);
                bigBorder.setBackground(selectColor);
            }

            tab_panel.add(button);

            buttons.add(button);
        }

        setVisible(true);

    }

    public void resetButtonColors()
    {
        for (JButton but : buttons)
        {
            but.setBackground(tab_color);
            but.setForeground(Color.black);
        }

        for (JPanel border: border_panels)
        {
            border.setBackground(back_color);
        }

        for (JPanel topborder: topBorderCollection)
        {
            topborder.setBackground(tab_color);
        }
    }

    public void addImagePaneEventListener(ImagePaneEventListener listener)
    {
        listenerList.add(ImagePaneEventListener.class, listener);
    }

    void setTabBorder(int tab_border) {
        this.tab_border = tab_border;
    }

    void setPanelBorder(int panelborder)
    {
        this.panelborder = panelborder;
    }

    void setTextOrientation(int HoriTextPos, int VertiTextPos) {
        for(JButton button : buttons)
        {
            button.setHorizontalTextPosition(HoriTextPos);
            button.setVerticalTextPosition(VertiTextPos);
        }
        this.validate();
        this.repaint();
    }



}
